/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iqq.ui;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import iqq.comm.Auth;
import iqq.service.LoginService;
import iqq.util.ErrorMessage;
import iqq.util.Log;
import iqq.util.ThreadUtil;

/**
 *
 * @author chenzhihui
 */
public class MainFrame extends javax.swing.JFrame {

    private static MainFrame mainFrame;
    private Map map = new HashMap();

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        setFont(new Font("宋体 ", Font.PLAIN, 15));
        this.setTitle("iQQ for " + System.getProperty("os.name"));
        mainFrame = this;
        this.setContentPane(new LoginPanel());
        this.setIconImage(Toolkit.getDefaultToolkit().createImage(
                getClass().getResource("/iqq/res/images/icon/bigIcon.png")));
    }

    public static MainFrame getMainFrame() {
        return mainFrame;
    }

    public void setIBackground(String path) {
        if (path != null) {
            ImageIcon img = new ImageIcon(this.getClass().getClassLoader().getResource(path));
            JLabel background = new JLabel(img);
            background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
            this.getLayeredPane().removeAll();
            this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
            this.validate();
        } else {
            this.getLayeredPane().removeAll();
            this.validate();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("iQQ");
        setBounds(new java.awt.Rectangle(1000, 100, 0, 0));
        setForeground(java.awt.Color.white);
        setName("QQ");
        setPreferredSize(new java.awt.Dimension(255, 555));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowIconified(java.awt.event.WindowEvent evt) {
                formWindowIconified(evt);
            }
        });

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        if (Auth.getPsessionid() == null) {
            ThreadUtil.shutdown();
            System.exit(0);
            return;
        }
        int optionType = JOptionPane.showConfirmDialog(null, "按 [是] 退出程序，[否] 隐藏到系统托盘", "是否退出程序？", JOptionPane.YES_NO_OPTION);
        Log.println("optionType:" + optionType + " yes:" + JOptionPane.YES_OPTION + " no:" + JOptionPane.NO_OPTION);
        if (optionType == JOptionPane.YES_OPTION) {
            ThreadUtil.shutdown();
            System.exit(0);
        } else {
            if (TrayFactory.getInstance().isIsSupported()) {
                this.setVisible(false);
            } else {
                ErrorMessage.show("系统不支持系统托盘！");
            }
        }
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    }//GEN-LAST:event_formWindowClosing

    private void formWindowIconified(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowIconified
        // TODO add your handling code here:
        if (TrayFactory.getInstance().isIsSupported()) {
            this.setExtendedState(JFrame.NORMAL);
            this.setState(JFrame.NORMAL);
            this.setVisible(false);
        }
    }//GEN-LAST:event_formWindowIconified

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        this.setVisible(false);
        LoginService.getInstance().logout();
        ThreadUtil.shutdown();
    }//GEN-LAST:event_formWindowClosed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    private void initSystemTray() {
        com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.3F);
        TrayFactory.getInstance().createTray(this, "/iqq/res/images/icon/webqq_icon.png", "你好，Java!\nHello,Java!");
    }
}
